package com.ljh;

import com.ljh.article.Aenemy;

import com.ljh.article.BaseAttack;
import com.ljh.article.Attackweapeon;
import com.ljh.creature.*;
import com.ljh.handler.*;

import java.util.HashMap;
import java.util.Scanner;

public class Sense {
    Person person = new Person();

    MonsterSkeleton monsterSkeleton = new MonsterSkeleton();
    MonsterMud monsterMud = new MonsterMud();
    MonsterBat monsterBat = new MonsterBat();
    MonsterBodyguard monsterBodyguard = new MonsterBodyguard();
    MonsterDragon MonsterDragon = new MonsterDragon();

    MonsterSet monsterSet = new MonsterSet();
    RoomSet roomSet = new RoomSet();

    private HashMap<String, Handler> handlders =new HashMap<String, Handler>();



    public Sense(){
        handlders.put("go", new HandlerGo(this));
        handlders.put("bye", new HandlerBye(this));
        handlders.put("help",new HandlerHelp(this));
        handlders.put(("chop"),new HandlerChop(this));

        initRoom();
    }



    public void initSence(){
        initPerson();
        initMonster();
        initRoom();
    }

    public void initRoom(){
        Room room1,room2,room3,room4,room5,room6;
        room1 = new Room("r1","墓地");
        room2 = new Room("r2","沼泽");
        room3 = new Room("r3","山洞");
        room4 = new Room("r4","宫殿外面");
        room5 = new Room("r5","宫殿内部");
        room6 = new Room("r6","成功逃出");

        room1.setExits("r2",room2);
        room2.setExits("r3",room3);
        room3.setExits("r4",room4);
        room4.setExits("r5",room5);
        room5.setExits("r6",room6);

        roomSet.getRooms().add(room1);
        roomSet.getRooms().add(room2);
        roomSet.getRooms().add(room3);
        roomSet.getRooms().add(room4);
        roomSet.getRooms().add(room5);
        roomSet.getRooms().add(room6);

        person.setCurrentRoom(room1);
    }

    public void goRoom(String direction)
    {
        Room nextRoom = person.getCurrentRoom().getExit(direction);

        if (nextRoom == null) {
            System.out.println("没有这个房间，请重新输入！");
        }
        else {
            person.setCurrentRoom(nextRoom);
        }
        showPromptAndProcess();
    }

    public void initPerson(){
        //
        person.setId("p1");
        person.setDescription("穿越者");
        person.setHp(200);
        person.setMaxhp(200);
        person.setLv(1);
        person.setExp(0);
        person.setExpVR(100);
        //防具
        person.setCurrentWeapon(new Aenemy("A1","布衣",10));
        person.getArticleWeapons().add(0,person.getCurrentWeapon());
        //武器
        person.setCurrentWeapon(new Attackweapeon("k1","拳头",-30));
        person.getArticleWeapons().add(1,person.getCurrentWeapon());




    }

    public void initMonster(){

        monsterSkeleton.setId("kulouguai");
        monsterSkeleton.setDescription("骷髅怪");
        monsterSkeleton.setHp(50);
        monsterSkeleton.setLeftExp(60);
        monsterSkeleton.setCurrentRoom(roomSet.getRooms().get(0));
        monsterSkeleton.setCurrentWeapon(new BaseAttack("bone","骨头",-10));

        monsterMud.setId("nibaguai");
        monsterMud.setDescription("泥巴怪");
        monsterMud.setHp(80);
        monsterMud.setLeftExp(80);
        monsterMud.setCurrentRoom(roomSet.getRooms().get(1));
        monsterMud.setCurrentWeapon(new BaseAttack("nut","泥巴",-20));


        monsterBat.setId("bianfuzhanshi");
        monsterBat.setDescription("蝙蝠战士");
        monsterBat.setHp(100);
        monsterBat.setLeftExp(100);
        monsterBat.setCurrentRoom(roomSet.getRooms().get(2));
        monsterBat.setCurrentWeapon(new BaseAttack("knife","大刀",-50));

        monsterBodyguard.setId("shiwei");
        monsterBodyguard.setDescription("侍卫");
        monsterBodyguard.setHp(120);
        monsterBodyguard.setLeftExp(90);
        monsterBodyguard.setCurrentRoom(roomSet.getRooms().get(3));
        monsterBodyguard.setCurrentWeapon(new BaseAttack("spear","长矛",-40));

        MonsterDragon.setId("longwnag");
        MonsterDragon.setDescription("龙王");
        MonsterDragon.setHp(200);
        MonsterDragon.setCurrentRoom(roomSet.getRooms().get(4));
        MonsterDragon.setCurrentWeapon(new BaseAttack("Fire stick","火杖",-50));


        monsterSet.getMonsters().add(0, monsterSkeleton);
        monsterSet.getMonsters().add(1, monsterMud);
        monsterSet.getMonsters().add(2, monsterBat);
        monsterSet.getMonsters().add(3, monsterBodyguard);
        monsterSet.getMonsters().add(4, MonsterDragon);

    }

    public void printWelcome() {
        System.out.println();
        System.out.println("你来到了一个新的国度");
        System.out.println("这个国家的公主被龙王抢走");
        System.out.println("国王希望你能够救出公主");
        System.out.println("出发吧少年！");
        System.out.println("不知道怎么玩？输入 help试试看！");
        System.out.println();
        showPromptAndProcess();
    }

    public void showPromptAndProcess(){
        boolean flag = false;
        System.out.println("==============================================================================");
        System.out.print("此处的敌人有:  ");//敌人属性
        for (Monster monsters: monsterSet.getMonsters()
             ) {
            if (monsters.getCurrentRoom().getId().equals(person.getCurrentRoom().getId())){
                if (monsters.getHp() <= 0) {//记录敌人死亡
                    monsters.setHp(0);
                    flag = true;
                    person.getEnemyExp(monsters);//人获取经验值
                    person.upGrade();
                }
                System.out.println(monsters.getId()+"  "+monsters.getDescription()+"  血量: "+monsters.getHp()+
                        "  攻击力: "+-monsters.getCurrentWeapon().getEffectValue());
                break;
            }
        }
        System.out.print(person.getDescription()+" 你目前在  " + person.getCurrentRoom());//你的状态
        System.out.println("  你的状态: 血量: "+person.getHp()+"  等级: "+person.getLv()+"  当前经验值: "+person.getExp()+
                "  武器: "+person.getCurrentWeapon().getDescription()+
                "  攻击力: "+-person.getCurrentWeapon().getEffectValue()+
                "  防具: " +person.getArticleWeapons().get(0).getDescription()+"  防御力: "
                +person.getArticleWeapons().get(0).getEffectValue());

        if (flag)
            ProcessOptimization();
        System.out.println("==============================================================================");
    }

    public void ProcessOptimization(){
            System.out.println("当前房间敌人已死亡，可前往下一关！");
            System.out.print("你可以前往 ");
            System.out.print(person.getCurrentRoom().getExitDesc());
            System.out.println(roomSet.getRooms().get(roomSet.getRooms().indexOf(person.getCurrentRoom())+1));
    }

    public void play(){
        Scanner in = new Scanner(System.in);
        while (true) {
            String line = in.nextLine();
            String[] words = line.split(" ");
            Handler handler = handlders.get(words[0]);
            String value = "";
            if (words.length > 1){
                value = words[1];
            }

            if (monsterSet.getMonsters().get(4).getHp() == 0 ){
                //结束语句
                System.out.println("恭喜您已经通关完成！");
                break;
            }
            else if (handler != null) {
                handler.doCmd(value);
                if (handler.isBye())
                    break;
            }
            else {
                System.out.println("输入错误命令，重新输入。");
            }
        }
        System.out.println("感谢您的游玩！");
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public MonsterSkeleton getMonsterSkeleton() {
        return monsterSkeleton;
    }

    public void setMonsterSkeleton(MonsterSkeleton monsterSkeleton) {
        this.monsterSkeleton = monsterSkeleton;
    }

    public MonsterMud getMonsterMud() {
        return monsterMud;
    }

    public void setMonsterMud(MonsterMud monsterMud) {
        this.monsterMud = monsterMud;
    }

    public MonsterBat getMonsterBat() {
        return monsterBat;
    }

    public void setMonsterBat(MonsterBat monsterBat) {
        this.monsterBat = monsterBat;
    }

    public MonsterBodyguard getMonsterBodyguard() {
        return monsterBodyguard;
    }

    public void setMonsterBodyguard(MonsterBodyguard monsterBodyguard) {
        this.monsterBodyguard = monsterBodyguard;
    }

    public MonsterDragon getMonsterDragon() {
        return MonsterDragon;
    }

    public void setMonsterDragon(MonsterDragon monsterDragon) {
        this.MonsterDragon = monsterDragon;
    }

    public MonsterSet getMonsterSet() {
        return monsterSet;
    }

    public void setMonsterSet(MonsterSet monsterSet) {
        this.monsterSet = monsterSet;
    }

    public RoomSet getRoomSet() {
        return roomSet;
    }

    public void setRoomSet(RoomSet roomSet) {
        this.roomSet = roomSet;
    }

    public HashMap<String, Handler> getHandlders() {
        return handlders;
    }

    public void setHandlders(HashMap<String, Handler> handlders) {
        this.handlders = handlders;
    }
}
